package com.oa.admin.service.impl;

import com.joneying.common.annotation.CommonService;
import com.oa.core.PageResult;
import com.oa.admin.entity.Inventory;
import com.oa.admin.mapper.InventoryMapper;
import com.oa.admin.service.IInventoryService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.core.utils.DateUtil;
import com.oa.core.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 库存表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory> implements IInventoryService {

    private final InventoryMapper inventoryMapper;

    @Autowired
    public InventoryServiceImpl(InventoryMapper inventoryMapper) {
        this.inventoryMapper = inventoryMapper;
    }

    @Override
    @CommonService
    public PageResult<Inventory> findList(Integer pageNum, Integer pageSize) {
        if(pageNum == null){
            List<Inventory> list = inventoryMapper.findList();
            return new PageResult<>(list);
        }else{
            // 当前页，总条数 构造 page 对象
            Page<Inventory> page = new Page<>(pageNum, pageSize);
            //查询
            List<Inventory> list = inventoryMapper.findList(page);
            return new PageResult<>(list);
        }
    }

    /**
    * 根据id查询
    */
    @Override
    @CommonService
    public Inventory findInventoryById(String id){
        return inventoryMapper.findById(id);
    }

    @Override
    public PageResult<Inventory> findInventoryList(Integer pageNum, Integer pageSize, String itemName, String warningStatus) {
        if(pageNum == null){
            List<Inventory> list = inventoryMapper.findList();
            return new PageResult<>(list);
        }else{
            // 当前页，总条数 构造 page 对象
            Page<Inventory> page = new Page<>(pageNum, pageSize);
            //查询
            List<Inventory> list = inventoryMapper.findInventoryList(page, itemName, warningStatus);
            return new PageResult<>(list);
        }
    }

    @Override
    public void export(HttpServletResponse response) {
        // 当前页，总条数 构造 page 对象
        Page<Inventory> page = new Page<>();
        //查询
        List<Inventory> list = inventoryMapper.findInventoryList(page, null, null);
        String[] rows = {"id", "物品名称", "品牌", "型号规格", "单位", "单价", "预警数量", "应存数量", "现存数量", "图片"};
        try {
            ExcelUtil.downloadExcel("库存列表" + DateUtil.getDayMonth(), rows, list, "库存列表" + DateUtil.getDayMonth()+".xls", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
